package com.lvhx.springboot.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.lvhx.springboot.entity.User;
import com.lvhx.springboot.entity.UserExample;
import com.lvhx.springboot.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
public class UserController {

    @Resource
    private UserMapper userMapper;

    @GetMapping("/getUserLi st")
    public String getUserList() {

        // 这边放值，logback配置文件中引用
        MDC.put("SOFA-TraceId", "AAAAAAAAA");

        log.info("getUserList");

        UserExample example = new UserExample();
        List<User> userList = userMapper.selectByExample(example);

        User user = new User();
        Random random = new Random();
        user.setId(random.nextLong());
        user.setName(IdUtil.simpleUUID());
        user.setPassword(IdUtil.simpleUUID());
        userMapper.insertSelective(user);


        return JSON.toJSONString(userList);
    }
}
